<?php
	
function list_biaya_jabatan(){

//PASANG JAVASCRIPT
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com='.$_GET['com'].'&task=delete_biaya_jabatan&id=\'+id;
	}
	return false;
}

');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Keperluan'=>array('style'=>'border-bottom:2px solid;width:20%'), 
		'Komponen Biaya'=>array('style'=>'border-bottom:2px solid;25%'), 
		'Batas Jam'=>array('style'=>'border-bottom:2px solid;5%'), 
		'Dokter (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;15%'), 
		'Perawat (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;15%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);

	$query = "SELECT * FROM spd_biaya_jabatan a
		INNER JOIN  spd_biaya b ON b.biaya_id = a.biaya_id 
		INNER JOIN  spd_keperluan c ON c.keperluan_id = a.keperluan_id 
		ORDER BY c.label_keperluan,b.label_biaya ASC";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit_biaya_jabatan&id='.$ey['biaya_jabatan_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['biaya_jabatan_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'keperluan'=>  $ey['label_keperluan'] ,   
			'biaya'=>  $ey['label_biaya'] ,   
			'jam'=>  '>'.$ey['batas_jam'] ,   
			'dokter'=>  position_text_align( rp_format($ey['nominal_dokter']) ,   'right'),
			'perawat'=>  position_text_align( rp_format($ey['nominal_perawat']) ,   'right'),
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);
	return table_builder($header , $datas , 5 , false );
}


function form_biaya_jabatan($id = 0){
	$view = form_header( "devisi" , "devisi"  );
 	$fields = my_get_data_by_id('spd_biaya_jabatan','biaya_jabatan_id',$id);
	
	$atemps = array();
	$query = "SELECT * FROM spd_keperluan ORDER BY label_keperluan";
	$re = my_query($query);
	while($row = my_fetch_array($re) ){
		$atemps[$row['keperluan_id']] =$row['label_keperluan'];
	}
	$temp = array(
		'name'=>'keperluan_id',
		'id'=>'keperluan_id',
		'value'=>isset($_POST['keperluan_id']) ? $_POST['keperluan_id'] : $fields['keperluan_id'] 
		);
	$form_temp = form_dropdown($temp , $atemps);
	$view .= form_field_display( $form_temp   , "Keperluan");

	$atemps = array();
	$query = "SELECT * FROM spd_biaya ORDER BY label_biaya";
	$re = my_query($query);
	while($row = my_fetch_array($re) ){
		$atemps[$row['biaya_id']] =$row['label_biaya'];
	}
	$temp = array(
		'name'=>'biaya_id',
		'id'=>'biaya_id',
		'value'=>isset($_POST['biaya_id']) ? $_POST['biaya_id'] : $fields['biaya_id'] 
		);
	$form_temp = form_dropdown($temp , $atemps);
	$view .= form_field_display( $form_temp   , "Komponen Biaya");
	$temp = array(
		'name'=>'batas_jam',
		'value'=>( isset($_POST['batas_jam']) ? $_POST['batas_jam'] : $fields['batas_jam'] ),
		'id'=>'batas_jam',
		'type'=>'text',
		'size'=>'5'
	);
	$form_temp = form_dynamic($temp);
	$view .= form_field_display( '> '.$form_temp.' jam'  , "Batas Jam" );	

	$temp = array(
		'name'=>'nominal_dokter',
		'value'=>( isset($_POST['nominal_dokter']) ? $_POST['nominal_dokter'] : rp_format($fields['nominal_dokter']) ),
		'id'=>'nominal_dokter',
		'type'=>'text',
		'size'=>'25'
	);
	$form_temp = form_dynamic($temp);
	$view .= form_field_display( 'Rp. '.$form_temp  , "Nominal untuk Dokter" );	
	$temp = array(
		'name'=>'nominal_perawat',
		'value'=>( isset($_POST['nominal_perawat']) ? $_POST['nominal_perawat'] : rp_format($fields['nominal_perawat']) ),
		'id'=>'nominal_perawat',
		'type'=>'text',
		'size'=>'25'
	);
	$form_temp = form_dynamic($temp);
	$view .= form_field_display( 'Rp. '.$form_temp  , "Nominal untuk Perawat" );	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function biaya_jabatan_validate($id){
	$errsubmit = false;
	$err = array(); 
	/*
	if((int)$_POST["jenis_non_karyawan_id"] == 0 ){
		$errsubmit = true;
		$err[] = "Jenis non karyawan belum di isi";	
	}
	
	if((int)$_POST["biaya_id"] == 0 ){
		$errsubmit = true;
		$err[] = "Biaya belum di isi";	
	}
	 
	$query="SELECT jenis_non_karyawan_biaya_id FROM spd_jenis_non_karyawan_biaya 
		WHERE jenis_non_karyawan_id=".$_POST["jenis_non_karyawan_id"].
		" AND biaya_id=".$_POST["biaya_id"].
		" AND jenis_non_karyawan_biaya_id<>".$id; 

	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Golongan dan biaya sudah ada";	
	}		
	 
	if((int)$_POST["nominal"] == 0	){
		$errsubmit = true;
		$err[] = "Nominal belum benar";	
	}
	*/
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function biaya_jabatan_submit($id){
	$datas = array(
		'keperluan_id'=>my_type_data_int( $_POST['keperluan_id'] ),
		'biaya_id'=>my_type_data_int( $_POST['biaya_id'] ),
		'batas_jam'=>my_type_data_str( $_POST['batas_jam'] ),
		'nominal_dokter'=>my_type_data_str( set_text_as_number($_POST['nominal_dokter']) ),
		'nominal_perawat'=>my_type_data_str( set_text_as_number($_POST['nominal_perawat']) ),
	);
	if($id > 0){
		return my_update_record('spd_biaya_jabatan' ,'biaya_jabatan_id' , $id , $datas);
	}
	
	return my_insert_record('spd_biaya_jabatan' , $datas);
}

function delete_biaya($id){
	$query= "DELETE FROM spd_biaya_jabatan WHERE biaya_jabatan_id = {$id}";
	return my_query($query);
} 